//
//  VipCardInfoViewController.m
//  fitnessmanage
//
//  Created by 众拳科技 on 2017/11/1.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//  会员卡详情

#import "VipCardInfoViewController.h"
#import "VipCardInfoView.h"
#import "VipCardRecordViewController.h"
#import "BindVipViewController.h"
#import "ModifyValidityViewController.h"
#import "VipCardLeaveViewController.h"
#import "FeeDeductionViewController.h"
#import "VipCardFormatViewController.h"
#import "VipCardInfoTableViewCell.h"
#import "HDAlertView.h"
#import "ModifyValidityViewController.h"
#import "VipCardViewController.h"

@interface VipCardInfoViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,weak)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataArray;

@property(nonatomic,strong)NSMutableArray *titleArray;

@property(nonatomic,strong)NSString *useShopName;

@end

@implementation VipCardInfoViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];

    self.title = @"会员卡详情";
    
    self.view.backgroundColor = BackGroundColor;
    
    //右侧图片 icon_nbarmore
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"icon_nbarmore"]
                                                                               imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(rightBtnClick)];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    
    [self createSubView];
}

-(void)createSubView{
    
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.showsVerticalScrollIndicator = NO;
    //tableView.estimatedRowHeight = 200;
    [tableView registerClass:[VipCardInfoTableViewCell class] forCellReuseIdentifier:@"VipCardInfoTableViewCell"];
    tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    tableView.backgroundColor = BackGroundColor;
    [self.view addSubview:tableView];
    [tableView mas_remakeConstraints:^(MASConstraintMaker *make) {        
        make.edges.mas_equalTo(self.view);
    }];
    self.tableView = tableView;
    
    VipCardInfoView *vipCardInfoView = [[VipCardInfoView alloc] init];
    
    vipCardInfoView.selectRow = self.selectRow;
    
    //扣费按钮点击
    [vipCardInfoView.chargeBtn addTarget:self action:@selector(feeDeductionBtnClick)];
    //请假按钮点击
    [vipCardInfoView.leaveBtn addTarget:self action:@selector(leaveBtnClick)];
    //充值按钮点击
    [vipCardInfoView.rechargeBtn addTarget:self action:@selector(rechargeBtnClick)];
    
    [self.view addSubview:vipCardInfoView];
    
    [vipCardInfoView mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.top.mas_equalTo(tableView);
        make.left.width.mas_equalTo(self.view);
    }];
    [vipCardInfoView setNeedsLayout];
    [vipCardInfoView layoutIfNeeded];

    vipCardInfoView.frame = CGRectMake(0, 0, self.view.width, vipCardInfoView.height);
    tableView.tableHeaderView = vipCardInfoView;

            
    int MemberCardType = [self.vipCardModel.MemberCardType intValue];
    
    //次卡储值类型（315055）
    if (MemberCardType == Time) {
        
        vipCardInfoView.cardNameLabel.text = [NSString stringWithFormat:@"%@（%@）",self.vipCardModel.MemberCardName,self.vipCardModel.MemberCardNum];
        
        vipCardInfoView.priceLabel.text = [NSString stringWithFormat:@"%@次",self.vipCardModel.Times];
    }
    //期限卡
    else if (MemberCardType == Term) {
        
        vipCardInfoView.cardNameLabel.text = [NSString stringWithFormat:@"%@（%@）",self.vipCardModel.MemberCardName, self.vipCardModel.MemberCardNum];
        
        NSString *endTime = self.vipCardModel.EndTime;
        
        NSDateComponents *components = [NSString getDateDifference:endTime];
        
        vipCardInfoView.priceLabel.text = [NSString stringWithFormat:@"%ld天",labs(components.day)];
    }
    //储值
    else{
        
        vipCardInfoView.cardNameLabel.text = [NSString stringWithFormat:@"%@（%@）",self.vipCardModel.MemberCardName,self.vipCardModel.MemberCardNum];
        
        vipCardInfoView.priceLabel.text = [NSString stringWithFormat:@"%@元",self.vipCardModel.TotalFee];
    }
    
    if (![self.vipCardModel.EndTime isEmptyString]) {
        
        vipCardInfoView.validTimeLabel.text = [NSString stringWithFormat:@"%@ - %@",self.vipCardModel.JoinTime,self.vipCardModel.EndTime];
    }
    else{
        
        vipCardInfoView.validTimeLabel.text = @"没有期限";
    }
    
    vipCardInfoView.cardIdLabel.text = [NSString stringWithFormat:@"ID:%@",self.vipCardModel.ID];
    
    NSString *shopName = @"";
    
    for (NSString *name in self.vipCardModel.ShopName) {
        
        shopName = [NSString stringWithFormat:@"%@%@、",shopName,name];
    }
    
    self.useShopName = shopName?[shopName substringWithRange:NSMakeRange(0, shopName.length - 1)]:@"";
}

#pragma mark UITableViewDataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 4;
}

#pragma mark UITableViewDelegate

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 15;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.00001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 80;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    VipCardInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VipCardInfoTableViewCell"];
    
    
    cell.title = [self.titleArray objectAtIndex:indexPath.row];
    
    if (indexPath.row == 0) {
        
        cell.detailLabel.text = self.vipCardModel.MemberName;
    }
    else if (indexPath.row == 1){
        
        cell.detailLabel.text = self.useShopName;
    }
    else if (indexPath.row == 2){
        
        cell.detailLabel.text = @"消费记录";
    }
    else{
        
        cell.detailLabel.text = self.vipCardModel.MemberCardNum;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        
        BindVipViewController *bindVipVC = [[BindVipViewController alloc] init];
        
        [self.navigationController pushViewController:bindVipVC animated:YES];
    }
    
    if (indexPath.row == 1) {
    
        [MBProgressHUD showText:@"请到会员通卡里面去修改"];
    }
    
    if (indexPath.row == 2) {
        
        VipCardRecordViewController *recordVC = [[VipCardRecordViewController alloc] init];
        recordVC.vipCardModel = self.vipCardModel;
        
        [self.navigationController pushViewController:recordVC animated:YES];
    }
    
}

#pragma mark 充值按钮点击
-(void)rechargeBtnClick{
    
    [MBProgressHUD showText:@"暂未开通"];
    
    VipCardFormatViewController *vipCardFromatVC = [[VipCardFormatViewController alloc] init];
    [self.navigationController pushViewController:vipCardFromatVC animated:YES];
}

#pragma mark 扣费按钮点击
-(void)feeDeductionBtnClick{
    
    [MBProgressHUD showText:@"暂未开通"];
    
    FeeDeductionViewController *feeDeuctionVC = [[FeeDeductionViewController alloc] init];
    [self.navigationController pushViewController:feeDeuctionVC animated:YES];
}

#pragma mark 请假按钮点击
-(void)leaveBtnClick{
    
    VipCardLeaveViewController *vipCardLeaveVC = [[VipCardLeaveViewController alloc] init];
    
    vipCardLeaveVC.vipCardModel = self.vipCardModel;
    
    [self.navigationController pushViewController:vipCardLeaveVC animated:YES];
}


#pragma mark 右侧按钮点击
-(void)rightBtnClick{
    
    HDAlertView *sheetView = [HDAlertView showActionSheetWithTitle:nil];
    
    sheetView.buttonsListStyle = HDAlertViewButtonsListStyleRows;
    
    [sheetView addButtonWithTitle:@"停卡" type:HDAlertViewButtonTypeCancel handler:^(HDAlertView *alertView) {
        
        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"是否确认停卡" message:@"停卡后:\n1.会员无法继续使用该会员卡;\n2.默认列表不展示以停卡的会员卡;\n3.停卡后可手动恢复该会员卡" preferredStyle:UIAlertControllerStyleAlert];//UIAlertControllerStyleAlert视图在中央
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确认停卡" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
            __weak typeof(self)weakSelf = self;
            
            [weakSelf stopCard];
            
        }];
        
        [alertController addAction:cancelAction];
        
        [alertController addAction:okAction];
        
        [self presentViewController:alertController animated:YES completion:nil];
                        
    }];
    
    [sheetView addButtonWithTitle:@"修改会员卡有效期" type:HDAlertViewButtonTypeDefault handler:^(HDAlertView *alertView) {
        
        //__weak typeof(self)weakSelf = self;
        
        
        [MBProgressHUD showText:@"暂未开通,敬请期待"];
        
//        ModifyValidityViewController *modifyVC = [[ModifyValidityViewController alloc] init];
        
//        [weakSelf.navigationController pushViewController:modifyVC animated:YES];
    }];
    
    [sheetView addButtonWithTitle:@"取消" type:HDAlertViewButtonTypeDefault handler:^(HDAlertView *alertView) {
        
    }];
    
    [sheetView show];
    
}

#pragma mark 停卡
-(void)stopCard{

    [MBProgressHUD showMessage:@"请求中..." toView:self.view];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    [params setObject:self.vipCardModel.MemberCardNum forKey:@"cardNum"];
    
    [HTTPTool POSTRaw:Post_StopMemberCard parameters:params
     
              success:^(id responseObject){
                  
                  [MBProgressHUD hideHUDForView:self.view];
                  
                  if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
                      
                      [MBProgressHUD showText:@"停卡成功"];
                      
                      [NotificationCenter postNotificationName:@"ReloadVenueCard" object:nil];
                      
                      for (UIViewController *tmpVC in self.navigationController.viewControllers) {
                          if ([tmpVC isKindOfClass:[VipCardViewController class]]) {
                              [self.navigationController popToViewController:tmpVC animated:YES];
                          }
                      }
                  }
              }
     
              failure:^(NSError *error){
                  
                  [MBProgressHUD hideHUDForView:self.view];
              }];
}

-(NSMutableArray *)dataArray{
    
    if (!_dataArray) {
        
        _dataArray = [[NSMutableArray alloc] init];
        
        [_dataArray addObject:@"绑定会员"];
        
        [_dataArray addObject:@"适用场馆"];
        
        [_dataArray addObject:@"消费记录"];
        
        [_dataArray addObject:@"实体卡号"];
    }
    
    return _dataArray;
}

-(NSMutableArray *)titleArray{
    
    if (!_titleArray) {
        
        _titleArray = [[NSMutableArray alloc] init];
        
        [_titleArray addObject:@"绑定会员"];
        
        [_titleArray addObject:@"适用场馆"];
        
        [_titleArray addObject:@"消费记录"];
        
        [_titleArray addObject:@"实体卡号"];
    }
    
    return _titleArray;
}
@end
